<?php

namespace App\Models\Chanjet;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class TplusPurchaseInvoiceInfo extends Model
{
    protected $table = 'tplus_purchase_invoice_info';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    public static function getList(Request $request, $memberinfo, $isgetall)
    {
        $manger_id = $memberinfo['id'] ?? 0;
        $cpid = $memberinfo['cpid'] ?? 0;

        //$sys_dpm_id = $memberinfo['sys_dpm_id'] ?? 0;

        $query = self::with("main")->where(['cpid' => $cpid]);

//        $company_name = $request->company_name;
//        if ($company_name != '') {
//            $query->where('company_name', 'like', '%' . $company_name . '%');
//        }

//        $start_time = strtotime($request->start_time) > 0 ? strtotime($request->start_time) : intval($request->start_time);
//        $end_time = strtotime($request->end_time) > 0 ? strtotime($request->end_time) : intval($request->end_time);
//        if ($start_time > 0 && $end_time > 0) {
//            $query->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time);
//        }

        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }

    }
    /**
     *  关联主表
     *
     */
    public function main()
    {
        return $this->belongsTo(TplusPurchaseInvoice::class,'main_id','id');
    }
}
